package sk.tramtatiri.pages.users;

import java.util.List;

import org.apache.wicket.PageParameters;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.AjaxSelfUpdatingTimerBehavior;
import org.apache.wicket.ajax.form.AjaxFormComponentUpdatingBehavior;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.Model;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.apache.wicket.util.time.Duration;

import sk.tramtatiri.data.dao.interfaces.RecipeDao;
import sk.tramtatiri.data.dataobjects.Recipe;

public class RecipeShowPanel extends Panel {
	private static final long serialVersionUID = 6469200865631147410L;

	@SpringBean
	private RecipeDao recipeDao;
	private List<Recipe> recipes;
	
	public RecipeShowPanel(String id) {
		super(id);

		Form<?> form = new Form<Void>("formRecipeShow");
		
		
//		List<IColumn<Recipe>> columns = new ArrayList<IColumn<Recipe>>();
//		columns.add(new PropertyColumn<Recipe>(new Model<String>("ID"), "id"));
//		columns.add(new PropertyColumn<Recipe>(new Model<String>("Meno"), "name", "name"));
//		columns.add(new PropertyColumn<Recipe>(new Model<String>("En. hodnota"), "totalCalories", "totalCalories"));
//		columns.add(new PropertyColumn<Recipe>(new Model<String>("Sacharidy"), "saccharides", "saccharides"));
//		columns.add(new PropertyColumn<Recipe>(new Model<String>("Tuky"), "fats", "fats"));
//		columns.add(new PropertyColumn<Recipe>(new Model<String>("Bielkoviny"), "proteins", "proteins"));
//		
//		form.add(add(new AjaxFallbackDefaultDataTable<Recipe>("dataTable", columns,
//            new RecipeProvider(recipeDao.findAll()), 8)));
		
		
		
		recipes = recipeDao.orderBy("name");
//		
//		RecipeProvider dataProvider = new RecipeProvider(recipes);
//		final DataView dataView = new DataView("row",dataProvider,20) {
//
//			@Override
//			protected void populateItem(final Item item) {
//				final Recipe recipe = (Recipe) item.getModelObject();
//				
//				PageParameters parameters = new PageParameters();
//				parameters.put("name", recipe.getName());
//				BookmarkablePageLink bpl = new BookmarkablePageLink("linkToRecipe", RecipePage.class, parameters);
//				bpl.add(new Label("recipeName", Model.of(recipe.getName())));
//				item.add(bpl);
//				item.add(new Label("recipeCalories", Model.of(recipe.getCalories())));
//				item.add(new Label("recipeSaccharides", Model.of(recipe.getSaccharides())));
//				item.add(new Label("recipeFats", Model.of(recipe.getFats())));
//				item.add(new Label("recipeProteins", Model.of(recipe.getProteins())));
//				
//				item.add(AttributeModifier.replace("class", new AbstractReadOnlyModel<String>()
//		                {
//		                    private static final long serialVersionUID = 1L;
//
//		                    @Override
//		                    public String getObject()
//		                    {
//		                        return (item.getIndex() % 2 == 1) ? "even" : "odd";
//		                    }
//		                }));
//			}
//		};
//		form.add(dataView);
		
//		RecipeProvider dataProvider = new RecipeProvider(recipes);
//		
//		IColumn[] columns = new IColumn[5];
//		columns[0] = new PropertyColumn(new Model<String>("Meno"), "name", "name");
//		columns[1] = new PropertyColumn(new Model<String>("En. hodnota"), "totalCalories", "totalCalories");
//		columns[2] = new PropertyColumn(new Model<String>("Sacharidy"), "saccharides", "saccharides");
//		columns[3] = new PropertyColumn(new Model<String>("Tuky"), "fats", "fats");
//		columns[4] = new PropertyColumn(new Model<String>("Bielkoviny"), "proteins", "proteins");
//		
//		DataTable table = new DataTable("dataTable", columns, dataProvider, 20);
//		
//		table.addBottomToolbar(new NavigationToolbar(table));
//		table.addTopToolbar(new HeadersToolbar(table, dataProvider));
//		form.add(table);


		ListView<Recipe> list = new ListView<Recipe>("row", recipes) {
			private static final long serialVersionUID = 1L;

			@Override
			protected void populateItem(ListItem<Recipe> item) {
				final Recipe recipe = (Recipe) item.getModelObject();
				
				final PageParameters parameters = new PageParameters();
				parameters.put("name", recipe.getName());
				BookmarkablePageLink bpl = new BookmarkablePageLink("linkToRecipe", RecipeDetailPage.class, parameters);
				bpl.add(new Label("recipeName", Model.of(recipe.getName())));
				item.add(bpl);
				item.add(new Label("recipeCalories", Model.of(recipe.getCalories())));
				item.add(new Label("recipeSaccharides", Model.of(recipe.getSaccharides())));
				item.add(new Label("recipeFats", Model.of(recipe.getFats())));
				item.add(new Label("recipeProteins", Model.of(recipe.getProteins())));
			}
		};
		
		form.add(list);
		
		add(form);
	}
}
